package com.leo;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
import javafx.util.Callback;
import javafx.util.StringConverter;

/**
 * @ClassName ComboBoxDemo
 * @Description
 * @Author wangss
 * @date 2020.05.25 11:45
 * @Version 1.0
 */
public class ComboBoxDemo extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        AnchorPane anchorPane = new AnchorPane();
        anchorPane.setStyle("-fx-background-color: #e7eeea");
        Scene scene = new Scene(anchorPane);

        Button button = new Button("点击");

        ComboBox<String> comboBox = new ComboBox<>();
        comboBox.getItems().addAll("item1", "item2", "item3", "item4", "item5");

        // 可以编辑
        comboBox.setEditable(true);
        // 文本提示
        comboBox.setPromptText("请输入内容");

        comboBox.setConverter(new StringConverter<String>() {
            @Override
            public String toString(String object) {
                return object;
            }

            @Override
            public String fromString(String string) {
                return null;
            }
        });

        comboBox.setCellFactory(new Callback<ListView<String>, ListCell<String>>() {
            @Override
            public ListCell<String> call(ListView<String> param) {
                MyListCell myListCell = new MyListCell();
                return myListCell;
            }
        });

        AnchorPane.setTopAnchor(comboBox, 10d);
        AnchorPane.setLeftAnchor(comboBox, 10d);
        AnchorPane.setTopAnchor(button, 10d);
        AnchorPane.setLeftAnchor(button, 260d);
        anchorPane.getChildren().addAll(comboBox, button);

        primaryStage.setScene(scene);
        primaryStage.setTitle("combobox");
        primaryStage.setWidth(625d);
        primaryStage.setHeight(500d);
        primaryStage.setResizable(false);
        primaryStage.show();
    }
}
